/*
 * @Description: HTTP返回实体
 * @Author: Haor
 * @Date: 2022-04-02 23:17:34
 * @LastEditTime: 2022-05-09 11:13:08
 * @LastEditors: Haor
 */

/**
 * 枚举状态码
 */
export enum Code {
	SUCCESS = 200, //表示已在响应中发出
	OTHER = 204, //（无内容） - 资源有空表示
	FAILED = 400, //操作失败
	AUTHORITIES = 401, //身份验证失败
	FORBIDDEN = 403, //无权限
	ERROR = 500 //通用服务器内部错误响应
}

/**
 * 枚举状态对应的消息
 */
export enum Message {
	SUCCESS = `操作成功`,
	FAILED = `操作失败`,
	PARAMETER = `参数错误`,
	AUTHORITIES = `登录过期`,
	FORBIDDEN = `无权访问`,
	ERROR = `服务异常`
}

/**
 * 标准的返回模板
 */
export interface HttpResTemplate {
	code: number
	data?: any
    nonce?: number
	error?: any
	message: string
}

/**
 * 分页对象
 */
export interface Pager {
	list: any[]
	total: number
	limit?: number
	page?: number
}

/**
 * 标准分页的返回模板
 */
export interface HttpPageTemplate {
	code: number
	data: Pager
	message: string
}

/**
 * 返回数据实体对象
 */
class HttpResultClass {
	/**
	 * 成功返回结果
	 * @param message
	 * @param data
	 */
	success(data?: any, message?: string): HttpResTemplate {
		return {
			code: Code.SUCCESS,
			data,
			message: message || Message.SUCCESS
		}
	}

	/**
	 * 带分页的数据对象
	 * @param {*} msg
	 * @param {*} code
	 * @param {*} data
	 * @param {*} total
	 * @param {*} page
	 * @param {*} limit
	 */
	pageData(data: Pager, message?: string, code?: number): HttpPageTemplate {
		return {
			code: code || Code.SUCCESS,
			data: data,
			message: message || Message.SUCCESS
		}
	}

	/**
	 * 代码分页(非数据库分页)
	 * @param {*} msg
	 * @param {*} code
	 * @param {*} data 数据列表
	 * @param {*} page 当前页
	 * @param {*} limit 每页大小
	 */
	totalPageData(data, page, limit) {
		const result: HttpResTemplate = {
			code: Code.SUCCESS,
			data: {
				list: [],
				limit,
				page,
				total: 0
			},
			message: Message.SUCCESS
		}

		//分页
		if (data && limit && page) {
			if (data && data.length > 0) {
				//索引
				const index = (page - 1) * limit
				for (let i = index; i < page * limit; i++) {
					if (data[i]) result.data.list.push(data[i])
				}
			}
			//总大小
			result.data.total = data.length
		} else {
			result.data = data
		}
		return result
	}

	/**
	 * 请求操作失败
	 * @param message
	 * @param code
	 * @param data
	 */
	failed(message?: string, code?: number, data?: any): HttpResTemplate {
		return {
			data: data || null,
			message: message || Message.FAILED,
			code: code || Code.FAILED
		}
	}

	/**
	 * 参数缺失
	 * @param {*} msg
	 * @param {*} code
	 * @param {*} data
	 */
	parameterExp(message?: string, code?: number, data?: any): HttpResTemplate {
		return {
			code: code || Code.OTHER,
			data,
			message: message || Message.PARAMETER
		}
	}

	/**
	 * 身份过期
	 * @param {*} msg
	 * @param {*} code
	 * @param {*} data
	 */
	authorities(message?: string, code?: number, data?: any): HttpResTemplate {
		return {
			code: code || Code.AUTHORITIES,
			data,
			message: message || Message.AUTHORITIES
		}
	}

	/**
	 * 无权访问
	 * @param {*} msg
	 * @param {*} code
	 * @param {*} data
	 */
	accessDenied(message?: string, code?: number, data?: any): HttpResTemplate {
		return {
			code: code || Code.FORBIDDEN,
			data,
			message: message || Message.FORBIDDEN
		}
	}

	/**
	 * 服务器内容错误
     * @param {number} nonce
	 * @param {*} msg
	 * @param {*} code
	 * @param {*} data
	 */
	serverError(nonce: number, error?: any, message?: string, code?: number): HttpResTemplate {
		return {
			error,
            nonce: nonce,
			code: code || Code.ERROR,
			message: message || Message.ERROR
		}
	}
}

export const HttpResult = new HttpResultClass()
